﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DwarfInterpreter
{
    class DwarfInterpreter
    {
        const char COMMAND_SEPARATOR = ';';

        Dwarf currentDwarf;

        public DwarfInterpreter()            
        {
            currentDwarf = null;
        }

        private void SelectDwarf(int index)
        {
            try
            {
                currentDwarf = DwarfWorld.Instance.GetDwarfAt(index - 1);
            }
            catch ( ArgumentOutOfRangeException e)
            {
                HandleException(e);
                return;
            }

            Console.WriteLine("Selected dwarf with id \'" + index + "\'");
        }

        public void InterpretLineRunCommand(string line)
        { 
            line = line.Trim().ToLower();
            
            string[] commands = line.Split(COMMAND_SEPARATOR);

            bool errState = false;
            foreach (string command in commands)
            {
                if (command.Length > 0)
                {
                    errState = RunCommand(command);
                    if (errState)
                    {
                        break;
                    }
                }                
            }
        }

        private bool RunCommand(string command)
        {
            command = StringUtils.AddBraketSpaces(command);
            command = StringUtils.RemoveExtraSpaces(command);
            string[] symbols = command.Split(' ');
                        
            Token t = Token.CreateToken(symbols);
            if (t is UseFileToken)
            {
                RunFile((t as UseFileToken).GetFilePath());
            }
            else if (t is SelectToken)
            {
                SelectDwarf((t as SelectToken).GetIndex());
            }            
            else
            {
                if (currentDwarf != null)
                {
                    try
                    {
                        t.Run(currentDwarf);
                    }
                    catch (KeyNotFoundException e)
                    {
                        HandleException(e);
                    }
                }
                else
                {
                    Console.WriteLine("Dwarf is not selected! Use \"select <id>;\" command.");
                    return true;
                }
            }

            return false;
        }

        private void RunFile(string filePath)
        {            
            string[] lines;
            try
            {
                lines = File.ReadAllLines(filePath);
            }
            catch (IOException e)
            {
                HandleException(e);
                return;
            }

            foreach (string line in lines)
            {
                InterpretLineRunCommand(line);
            }
        }

        private void HandleException(Exception e)
        {
            Console.WriteLine(e.Message);
        }        
    }
}